package com.xl.web.service.message;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xl.base.annotation.DataSource;
import com.xl.base.dao.BaseDao;
import com.xl.base.page.BasePage;
import com.xl.base.service.BaseService;
import com.xl.core.enums.SourceEnum;
import com.xl.web.dao.message.MessageBaseDao;
import com.xl.web.entity.message.MessageBase;

import jodd.util.StringUtil;

@Service
public class MessageBaseService<T extends MessageBase> extends BaseService<MessageBase> {

	@Autowired
	private MessageBaseDao<MessageBase> messageBaseDao;
	
	@Override
	public BaseDao<MessageBase> getDao() {
		return messageBaseDao;
	}
	
	/**
	 * 列表查询
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@DataSource(source = SourceEnum.TYPE_SLAVE)
	public List<MessageBase> queryList(BasePage page) throws Exception {
		Integer rowCount = messageBaseDao.queryCount(page);
		page.getPager().setRowCount(rowCount);
		return messageBaseDao.queryList(page);
	}

	/**
	 * 添加或修改
	 * @param letter
	 */
	public void saveOrUpdate(MessageBase messageBase){
		if(StringUtil.isBlank(messageBase.getId())){
			messageBase.preInsert();
			messageBaseDao.add(messageBase);
		}else{
			messageBase.preUpdate();
			messageBaseDao.updateBySelective(messageBase);
		}
	}
	/**
	 * 删除
	 * @param letter
	 * @throws IOException
	 */
	public void delete(MessageBase messageBase) throws IOException{
		messageBaseDao.delete(messageBase);
	}
	/**
	 * 数据条数
	 * @param t
	 * @return
	 */
	public int queryCount(T t){
		return messageBaseDao.queryCount(t);
	}
}